#!/usr/bin/python
# -*- coding: UTF-8 -*-

from monitor.base import MonitorBase
from utils.log.log import Log
from utils.smtp.smtp import Smtp
import psutil as p
from config.config import (
    COMMON_INTERVALTIME,
    ALARM_DISK_PERCENTAGE_LIMIT,
    ALARM_DISK_EXCEPT_MAX_NUM,
)
from utils.utils import bytes2human
from utils.utils import (
    get_host_ip,
    get_host_name,
    isWindows,
)

log = Log("disk monitor")


class DiskMonitor(MonitorBase):
    """docstring for MemMonitor"""

    def __init__(self):
        super(DiskMonitor, self).__init__(COMMON_INTERVALTIME)
        self.smtp = None
        self.num = 0

    def Init(self):
        """Init(self)"""
        log.Debug("disk monitor init")
        self.smtp = Smtp()
        self.num = 0

    def Check(self):
        """Check(self)"""
        log.Debug("disk monitor Check !!!!")
        dstpath = "/"
        d = p.disk_usage(dstpath)
        log.Debug(
            "disk info=",
            "total=%s used=%s free=%s per=%f"
            % (
                bytes2human(d.total),
                bytes2human(d.used),
                bytes2human(d.free),
                d.percent,
            ),
        )
        if d.percent >= ALARM_DISK_PERCENTAGE_LIMIT:
            self.num = self.num + 1
            log.Debug(
                "dick_percent except appear %d times and max ALARM_DISK_EXCEPT_MAX_NUM times=%d"
                % (self.num, ALARM_DISK_EXCEPT_MAX_NUM)
            )
            if self.num >= ALARM_DISK_EXCEPT_MAX_NUM:
                log.Debug("mem_percents=", d.percent)
                self.smtp.Send(
                    "server %s[%s] appear disk percentage limit max!!!!!!!!!!!!!!!!!!"
                    % (get_host_ip(), get_host_name())
                )
                self.num = 0
        else:
            self.num = 0
